package com.qdlc.p2p.biz.service.user.login;

import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.util.ValidateUtil;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;

/**
 * 登录方式-以手机号登录
 * 
 * @author xx
 * @version 2.0
 * @since 2014年2月11日
 */
public class MobilePhoneLoginWay implements LoginWay {
	private UserService userService;
	private UserIdentifyService userIdentifyService;
	private boolean enable = false;

	public MobilePhoneLoginWay(boolean enable) {
		super();
		this.enable = enable;
	}

	@Override
	public User doLogin(User user) {
		User u = null;
		if (enable && ValidateUtil.isPhone(user.getUserName())) {// 手机号
			userService = (UserService) BeanUtil.getBean("userService");
			u = userService.getUserByMobilePhone(user.getUserName());
			if(u!=null && u.getUserId()>0){
				userIdentifyService = (UserIdentifyService) BeanUtil.getBean("userIdentifyService");
				UserIdentify userIdentify = userIdentifyService.findByUserId(u.getUserId());
				if(userIdentify!=null && userIdentify.getMobilePhoneStatus()!=1){
					throw new UserException("手机号码还未认证，登录失败！", 1);
				}
			}
		}
		return u;
	}

}
